﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestoreSuperProgetto.Model;

namespace GestoreSuperProgetto
{
    public partial class UserEditor : UserControl
    {
        public UserEditor()
        {
            InitializeComponent();
        }

        private USER activeUser = null;

        public USER ActiveUser
        {
            get { return activeUser; }
            set { activeUser = value; if(activeUser!=null) UpdateUI(); }
        }

        private void UpdateUI()
        {
            groupBox.Enabled = true;
            usernameTxt.Text = activeUser.USERNAME;
            passwordTxt.Text = USER.Decrypt(activeUser.PASSWORD);
            svnUserTxt.Text = activeUser.SVNUSER;
            svnPasswordTxt.Text = USER.Decrypt(activeUser.SVNPASSWORD);
            adminUserChk.Checked = activeUser.ISADMIN==1?true:false;
            adminUserChk.Enabled = activeUser.ISADMIN == 1 ? true : false;
            defaultUnitTxt.Text = activeUser.DEFAULTDIR;
        }

        public void UpdateObject()
        {
            activeUser.USERNAME = usernameTxt.Text;
            activeUser.PASSWORD = USER.Encrypt(passwordTxt.Text);
            activeUser.SVNUSER = svnUserTxt.Text;
            activeUser.SVNPASSWORD = USER.Encrypt(svnPasswordTxt.Text);
            activeUser.ISADMIN = (long)(adminUserChk.Checked?1:0);
            activeUser.DEFAULTDIR = defaultUnitTxt.Text;
        }

        public USER CreateNewUser()
        {
            activeUser = new USER();
            activeUser.USERNAME = usernameTxt.Text;
            activeUser.PASSWORD = USER.Encrypt(passwordTxt.Text);
            activeUser.SVNUSER = svnUserTxt.Text;
            activeUser.SVNPASSWORD = USER.Encrypt(svnPasswordTxt.Text);
            activeUser.ISADMIN = (long)(adminUserChk.Checked?1:0);
            activeUser.DEFAULTDIR = defaultUnitTxt.Text;

            return activeUser;
        }

        public void ResetValue()
        {
            usernameTxt.Text = "";
            passwordTxt.Text = "";
            svnUserTxt.Text = "";
            svnPasswordTxt.Text = "";
            adminUserChk.Checked = false;
            defaultUnitTxt.Text = "C";
        }

        public bool IsModified
        {
            get
            {
                return ((usernameTxt.Text != activeUser.USERNAME) ||
                         (passwordTxt.Text != activeUser.PASSWORD) ||
                         (svnUserTxt.Text != activeUser.SVNUSER) ||
                         (svnPasswordTxt.Text != activeUser.SVNPASSWORD) ||
                         (adminUserChk.Checked != (activeUser.ISADMIN==1)) ||
                         (defaultUnitTxt.Text != activeUser.DEFAULTDIR));
            }
        }

        private void browseBtn_Click(object sender, EventArgs e)
        {
            string defaultDir = activeUser.DEFAULTDIR;

            folderBrowserDialog.SelectedPath = defaultDir;
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                defaultUnitTxt.Text = folderBrowserDialog.SelectedPath;
            }
        }
    }
}
